package com.kgc.xsProvider.service.ServiceImpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesOrders;
import com.kgc.xs.pojo.SalesOrdersDetail;
import com.kgc.xs.pojo.SalesQuotesDetail;
import com.kgc.xsProvider.dao.SalesOrdersMapper;
import com.kgc.xsProvider.service.SalesOrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SalesOrdersServiceImpl implements SalesOrdersService {

    @Autowired
    private SalesOrdersMapper salesOrdersMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public ResponseMsg selectSalesOrdersByPage(Map<String, Object> map) {
        logger.info("SalesOrdersServiceImpl selectSalesOrdersByPage params {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesOrders> pageInfo = new PageInfo<SalesOrders>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<SalesOrders> list = salesOrdersMapper.selectSalesOrdersByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int) page.getTotal());
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseMsg insertSalesOrders(Map<String, Object> map) {
        logger.info("SalesOrdersServiceImpl insertSalesOrders params {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();

        // 转换 SalesOrder 对象
        SalesOrders order = mapper.convertValue(
                map.get("salesOrders"),
                SalesOrders.class
        );
        List<?> rawDetails = (List<?>) map.get("details");

// 使用 ObjectMapper 转换集合类型
        List<SalesOrdersDetail> details = mapper.convertValue(
                rawDetails,
                new TypeReference<List<SalesOrdersDetail>>() {
                }
        );
        int result1 = salesOrdersMapper.insertSalesOrders(order);
        try {
            for (SalesOrdersDetail detail : details) {
                salesOrdersMapper.insertSalesOrdersDetail(detail);
            }
            responseMsg.setCode("200");
        } catch (Exception e) {
            responseMsg.setCode("201");
            return responseMsg;
        }
        if (result1 > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOrdersServiceImpl insertSalesOrders result1:{}", responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesOrders(Map<String, Object> map) {
        logger.info("SalesOrdersServiceImpl updateSalesOrders params {}", map.toString());
        deleteSalesOrders(map);
        ResponseMsg responseMsg = insertSalesOrders(map);
        logger.debug("SalesOrdersServiceImpl updateSalesOrders result1:{}", responseMsg);
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesOrders(Map<String, Object> map) {
        logger.info("SalesOrdersServiceImpl deleteSalesOrders params {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result1 = salesOrdersMapper.deleteSalesOrders(map);
        int result2 = salesOrdersMapper.deleteSalesOrdersDetail(map);
        if (result1 > 0&&result2 > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOrdersServiceImpl deleteSalesOrders result1:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesOrdersById(int id) {
        return null;
    }

    @Override
    public ResponseMsg checkSalesOrdersById(Map<String, Object> map) {
        logger.info("SalesOrdersServiceImpl checkSalesOrdersId params {}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        int result = salesOrdersMapper.checkSalesOrdersById(map);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOrdersServiceImpl checkSalesOrdersId result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesOrdersDetailByPage(Map<String, Object> map) {
        return null;
    }

    @Override
    public ResponseMsg selectSalesOrdersDetailById(int id) {
        return null;
    }

    @Override
    public ResponseMsg deleteSalesOrdersDetail(String xsOrderCode) {
        logger.info("SalesOrdersServiceImpl deleteSalesOrdersDetail params {}", xsOrderCode);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesOrdersByCode(String docCode) {
        logger.info("SalesOrdersServiceImpl selectSalesOrdersByCode params {}", docCode);
        ResponseMsg responseMsg = new ResponseMsg();
        SalesOrders salesOrders = salesOrdersMapper.selectSalesOrdersByCode(docCode);
        if (salesOrders != null) {
            responseMsg.setCode("200");
            responseMsg.setData(salesOrders);
        } else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOrdersServiceImpl selectSalesOrdersByCode result1:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesOrdersDetailByCode(String xsOrderCode) {
        logger.info("SalesOrdersServiceImpl selectSalesOrdersDetailByCode params {}", xsOrderCode);
        ResponseMsg responseMsg = new ResponseMsg();
        List<SalesOrdersDetail> list = salesOrdersMapper.selectSalesOrdersDetailByCode(xsOrderCode);
        responseMsg.setData(list);
        logger.debug("SalesOrdersServiceImpl selectSalesOrdersDetailByCode result1:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCheck(String code, Integer checkManId, String checkComment) {
        logger.info("SalesOrdersServiceImpl insertCheck params {}", code);
        ResponseMsg responseMsg = new ResponseMsg();
        int result1 = salesOrdersMapper.insertCheck(code, checkManId, checkComment);
        if (result1 > 0) {
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOrdersServiceImpl insertCheck result1:{}", responseMsg);
        return responseMsg;
    }
}
